
<template>
<theme-box title="激励添加">
    <form class="form-inline" @submit.prevent v-el:form>
        <div class="box-body">
            <form-input title="事件key" :required="true" name="eventKey"></form-input>
            <form-input title="事件值" :required="true" name="eventValue"></form-input>
        </div>
        <!-- /.box-body -->
        <div class="box-footer">
            <v-button type="reset" @click="reset">重置</v-button>
            <v-button type="submit" theme="primary" class="pull-right">确认</v-button>
        </div>
        <!-- /.box-footer -->
    </form>
</theme-box>
</template>

<script>
import 'plugins/jquery-validation';

var serialize = require('form-serialize');

export default {
    data: () => ({

    }),
    methods: {
        reset() {
            $("label.error").remove();
            this.$els.form.reset()
        }
    },
    ready() {
        var self = this;
        $(this.$els.form).validate({
            submitHandler: async function() {
                var _data = serialize(this.currentForm, {
                    hash: true,
                    empty: true
                });
                var oprId = JSON.parse(localStorage.getItem("id_token")).oprId
                _data.updOpr = oprId
                 var result = await self.$post({
                    url: "/api/v1"+ projectName_Url +"/plat/bestir/add",
                    data: _data
                })
                let data = {
                    title: "操作成功",
                    content: "是否进入激励列表查看?"
                }
                Midway.confirm(data).then(function () {
                    self.$route.router.go('/encourage/list');
                })
                self.reset()
            },

            errorPlacement: function(error, element) {
            console.log(element);
                if (element.attr("name") == "file") {
                    error.insertAfter(".file-item");
                } else {
                    error.insertAfter(element)
                }
            },
            rules: {
               eventKey: {
                    required: true,
                    stringMaxLength:8
                },
                eventValue:{
                    required: true,
                    int:true,
                    number:true,
                    min:1,
                    maxlength:3
                }
            },
            messages:{
              eventValue:{
                number:"必须是数字",
                int:"必须为整数"

              },
            }
        });
    }
}
</script>
